package logic

import (
	"context"
	"product-mall/models/mysql"

	"product-mall/productrpc/internal/svc"
	"product-mall/productrpc/productrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ListRateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewListRateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListRateLogic {
	return &ListRateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ListRateLogic) ListRate(in *productrpc.ListRateRequest) (*productrpc.ListRateResponse, error) {
	// todo: add your logic here and delete this line

	var rate []mysql.Rate
	mysql.DB.Debug().Where("product_id = ?", in.ProductId).Find(&rate)

	var rateList []*productrpc.ListRate

	for _, m := range rate {
		list := productrpc.ListRate{
			UserId:  m.UserId,
			Content: m.Content,
		}
		rateList = append(rateList, &list)
	}

	return &productrpc.ListRateResponse{List: rateList}, nil
}
